package uy.com.tsi2G7.superbet.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import uy.com.tsi2G7.superbet.integration.betfair.rss.Feed;
import uy.com.tsi2G7.superbet.integration.betfair.rss.FeedMessage;
import uy.com.tsi2G7.superbet.integration.betfair.rss.RSSFeedParser;

@ManagedBean(name = "rssNewsBean")
@ViewScoped
public class RSSNewsBean {
	private List<Feed> news;
	
	private List<Feed> refresh(){
		List<Feed> res=new ArrayList<Feed>();		
		RSSFeedParser parser = new RSSFeedParser("http://marca.feedsportal.com/rss/futbol_futbol_internacional.xml");		
		//RSSFeedParser parser = new RSSFeedParser("http://www.elpais.com.uy/formatos/rss/index.asp?seccion=deportes");
		Feed feed = parser.readFeed();
		Iterator<FeedMessage> i = feed.getEntries().iterator();
		while(i.hasNext()){
			FeedMessage message = i.next();
			message.setDescription(message.getDescription().substring(0,message.getDescription().indexOf("&")));
		}
		res.add(feed);		
		return res;
	}
	
	public List<Feed> getNews(){
		if (this.news == null){
			this.news = refresh();
		}
		return this.news;
	}

	public void setNews(List<Feed> news) {
		this.news = news;
	}
}
